﻿using MobileBSC_Data;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public class WeakSignalWriter
    {
        private const string INSERT_STMT = "INSERT INTO WeakSignal (Name, Description, Source) VALUES (@name, @description, @source);";
        private const string UPDATE_STMT = "UPDATE WeakSignal SET Name = @name, Description = @description, Source = @source WHERE WeakSignalID = @id;";
        private const string DELETE_STMT = "DELETE FROM WeakSignal WHERE WeakSignalID = @id;";

        public bool Write(WeakSignal data)
        {
            if (data == null)
                return false;

            return DbTool<WeakSignal>.ExecuteCommand(CreateWriteCommand(data));
        }
        public bool Delete(WeakSignal data)
        {
            if (data == null)
                return false;

            return DbTool<WeakSignal>.ExecuteCommand(CreateDeleteCommand(data.ID));
        }
        private SQLiteCommand CreateWriteCommand(WeakSignal data)
        {
            var result = new SQLiteCommand();
            result.CommandType = System.Data.CommandType.Text;

            if (data.ID > 0)
            {
                result.CommandText = UPDATE_STMT;
                result.Parameters.Add(new SQLiteParameter("@id", data.ID));
            }
            else
                result.CommandText = INSERT_STMT;

            result.Parameters.Add(new SQLiteParameter("@name", data.Name));
            result.Parameters.Add(new SQLiteParameter("@description", data.Description));
            result.Parameters.Add(new SQLiteParameter("@source", data.Source));

            return result;
        }

        private SQLiteCommand CreateDeleteCommand(long id)
        {
            var result = new SQLiteCommand();
            result.CommandType = System.Data.CommandType.Text;
            result.CommandText = DELETE_STMT;
            result.Parameters.Add(new SQLiteParameter("@id", id));

            return result;
        }
    }
}
